package com.example.testdemo;

import java.util.concurrent.TimeUnit;

/**
 * 模拟死锁场景
 */
public class DieLockTest {
       public  static  Object object1=new Object();     //临界资源
       public  static  Object object2=new Object();     //临界资源

}
class  D1 extends   Thread{

     DieLockTest lock=new DieLockTest();
    @Override
    public void run() {
        synchronized (lock.object1){
            System.out.println("===111");

            try {
                TimeUnit.SECONDS.sleep(1);
            }catch (Exception e){
                e.printStackTrace();
            }

            synchronized (lock.object2){
                System.out.println("=======222");
            }
        }
    }
}
class  D2 extends  Thread{

    DieLockTest lock=new DieLockTest();
    @Override
    public void run() {
        synchronized (lock.object2){
            System.out.println("=========333");

            try {
                TimeUnit.SECONDS.sleep(1);
            }catch (Exception e){
                e.printStackTrace();
            }

            synchronized (lock.object1){
                System.out.println("==============444");
            }
        }
    }
}

class  TheTestDemo{
    public static void main(String[] args) {

       Thread d1=new D1();
       Thread d2=new D2();

       d1.start();

       d2.start();
        /**
         * 一直在等待。。。。。。。死锁现象产生
         */
    }
}

